<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/5/20
 * Time: 16:16
 */
namespace app\admin\controller;
use app\models\Models;
use app\validate\GroupValidate;
use think\Config;
use think\Request;

/**
 * +-----------------------------------
 * Class Group
 * @package app\admin\controller
 * 角色分组列表
 * +------------------------------------
 */
class Group extends Check{
    /**
     * @return \think\response\View
     * 角色列表
     */
    public function groupList(){
        $group = new Models('auth_group');
        $rules = new Models('auth_rule');
        $list  = $group->_select(['id'=>['<>',Models::GROUP_ADMIN]]);
        if($list){
            foreach($list as $k=>$v){
                $rule_arr = '';
                $rule = $rules->getColumn(['id'=>['in',$v['rules']]],'title');
                if($rule){
                    foreach($rule as $k1=>$v1){
                        $rule_arr .= $v1.',';
                    }
                }
                $list[$k]['rules'] = rtrim($rule_arr,',');
            }
        }
        return view('groupList',['list'=>$list]);
    }

    /**
     * 添加角色分组
     * @return array|\think\response\View
     */
    public function groupAdd(){
        $rule = new Models('auth_rule');
        if(request()->isPost()){
            $post = input('param.');
            $group = new Models('auth_group');
            try{
                $post['rules'] = implode(',',$post['rules']);
                // 场景验证
                $validate = self::validate($post,'Group.groupAdd');
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                // 添加角色
                $bool = $group->_add($post);
                // 添加操作日志
                $log  = self::addLog('添加角色-'.$post['title']);
                if(!$bool || !$log){
                    throw new \LogicException('操作失败',1010);
                }
            }catch(\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $list = $rule->_select("p_id = 0");
        $rules = [];
        foreach($list as $v){
            $rules[$v['title']] = $rule->_select("p_id = {$v['id']}");
        }
        return view('groupAdd',['list'=>$rules]);
    }

    /**
     * @return array|\think\response\View
     * 更新角色分组
     */
    public function groupSave(){
        $group = new Models('auth_group');
        if(request()->isPost()){
            $post = input('param.');
            try{
                $post['rules'] = implode(',',$post['rules']);
                // 场景验证
                $validate = self::validate($post,'Group.groupAdd');
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                // 更新角色信息
                $bool = $group->_save($post);
                // 添加操作日志
                $log  = self::addLog('修改角色-'.$post['title']);
                if(false === $bool || !$log){
                    throw new \LogicException('操作失败',1010);
                }
            }catch(\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $get  = input('param.');
        $rule = new Models('auth_rule');
        $list = $rule->_select("p_id = 0");
        $rules = [];
        foreach($list as $v){
            $rules[$v['title']] = $rule->_select("p_id = {$v['id']}");
        }
        $info = $group->_find(['id'=>$get['id']]);
        return view('groupSave',['list'=>$rules,'info'=>$info]);
    }
}